package cn.sczhyt.modules.app.controller;

import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.R;
import cn.sczhyt.modules.app.annotation.Login;
import cn.sczhyt.modules.app.utils.FileReadOnlineUtils;
import cn.sczhyt.modules.szhy.entity.MeetingBriefReportEntity;
import cn.sczhyt.modules.szhy.service.MeetingBriefReportService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;


/**
 * 会议简报
 *
 * @author luojialin
 * @email 78357966@qq.com
 * @date 2021-04-22 13:31:44
 */
@RestController
@RequestMapping("app/meetingbriefreport")
public class AppMeetingBriefReportController {
    @Autowired
    private MeetingBriefReportService meetingBriefReportService;

    /**
     * 列表
     */
    @Login
    @RequestMapping("/list")
    public R list(@RequestParam String meetingId){

        List<MeetingBriefReportEntity> list = meetingBriefReportService.appFindAll(meetingId);

        return R.ok().put("data", list);
    }


    /**
     * 文件在线阅读
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation("PDF文件在线阅读")
    @RequestMapping("/fileReadOnline/{id}")
    public void fileReadOnline(HttpServletResponse response, @PathVariable("id") String id) throws IOException {


        MeetingBriefReportEntity reportEntity = meetingBriefReportService.getById(id);
        String filePath = reportEntity.getFilePath();
        String fileName = reportEntity.getFileName();
        String fileType = "pdf";
        FileReadOnlineUtils.fileReadOnline(response,filePath,fileName,fileType);
    }



    /**
     * 信息
     */
    @Login
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		MeetingBriefReportEntity meetingBriefReport = meetingBriefReportService.getById(id);

        return R.ok().put("data", meetingBriefReport);
    }

    /**
     * 保存
     */
    @Login
    @RequestMapping("/save")
    public R save(@RequestParam("file") MultipartFile[] files, @RequestParam String meetingId){

		meetingBriefReportService.saveMeetingBriefReport(files,meetingId);

        return R.ok();
    }


    /**
     * 文件下载
     */
    @Login
    @ApiOperation("文件下载")
    @GetMapping("/download")
    public R download(@RequestParam String id, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {

        meetingBriefReportService.download(id,response,request);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MeetingBriefReportEntity meetingBriefReport){
		meetingBriefReportService.updateById(meetingBriefReport);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){

		meetingBriefReportService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
